﻿using DataAccess;
using DataAccess.Entity;
using DataAccess.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC4.Controllers
{
    public class FirstController : Controller
    {
        //
        // GET: /First/

        public ActionResult Index()
        {
            return View();
        }

        [AllowAnonymous]
        [HttpPost]
        public JsonResult Index(FormCollection form)
        {
            try
            {
                if (string.IsNullOrEmpty(form["name"])) { return Json(new { status = false, msg = "不能空" }); }
                string name = form["name"];
                var firstRepository = new FirstRepository();
                //var firstRepository = new FIRST_TRepository();
                var first = firstRepository.FindEntity(m => m.Name.Equals(name));
                if (null == first)
                {
                    firstRepository.Insert(new First()
                    //firstRepository.Insert(new FIRST_T()
                    {
                        Id = Guid.NewGuid().ToString("N").ToUpper(),
                        Name = form["name"],
                        Status = 1,
                        CreationTime = DateTime.Now,
                        UpdateTime = DateTime.Now
                    });
                }
                else
                {
                    first.UpdateTime = DateTime.Now;
                    firstRepository.Update(first);
                }
                return Json(new { status = true, msg = "成功" });
            }
            catch (Exception ex)
            {
                return Json(new { status = false, msg = ex.Message });
            }
        }
        
        [AllowAnonymous]
        [HttpPost]
        public JsonResult Index2()
        {
            try
            {
                var firstRepository = new FirstRepository();
                //var firstRepository = new FIRST_TRepository();
                return Json(new { status = true, msg = firstRepository.Count() });
            }
            catch (Exception ex)
            {
                return Json(new { status = false, msg = ex.Message });
            }
        }
    }
}
